/**/
function addGoodsOrder(id, title) {
	var form = new Ext.form.FormPanel({
		id : 'form',
		frame : true,
		bodyBorder : false,
		labelAlign : 'right',
		monitorValid : true,
		height : 290,
		region : 'center',// 定位
		items : [{
			layout : 'column',
			border : false,
			items : [{
						xtype : 'hidden',
						name : 'deliveryFee',
						value : 0
					}, {
						columnWidth : .33,
						layout : 'form',
						border : false,
						items : [{
							xtype : 'dataDictionaryCmb',
							fieldLabel : "<span style='color:#F00'>*</span>订单类型",
							allowBlank : false,
							blankText : '订单类型不能为空',
							editable : false,
							autoLoad : false,
							anchor : '100%',
							hiddenName : 'orderType',
							dictionaryType : PmsUtil.good_order_type,
							listeners : {
								'select' : function(obj) {
									var orderType = obj.value;
									addOrderTypeShow(orderType);
								}
							}
						}]
					}, {
						columnWidth : .33,
						layout : 'form',
						border : false,
						items : [{
							       id:'addOrderForm_orderFinish',
									xtype : 'localcommonbox',
									anchor : '100%',
									fieldLabel : "<span style='color:#F00'>*</span>订单完成",
									hiddenName : 'orderFinish',
									loaclarr : [['0', '否'], ['1', '是']],
									value : '0'
								}]
					}, {
						columnWidth : .33,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'hidden',
									name : 'couponType',
									value : 2
								}, {
									xtype : 'textfield',
									anchor : '100%',
									fieldLabel : '优惠码',
									name : "couponCode"
								}]
					}]
		}, {
			layout : 'column',
			border : false,
			items : [{
				columnWidth : .33,
				layout : 'form',
				border : false,
				items : [{
							xtype : 'textfield',
							anchor : '100%',
							fieldLabel : "<span style='color:#F00'>*</span>收货人",
							allowBlank : false,
							blankText : '收货人不能为空',
							name : "consignee"
						}]
			}, {
				columnWidth : .33,
				layout : 'form',
				border : false,
				items : [{
							xtype : 'textfield',
							anchor : '100%',
							fieldLabel : "<span style='color:#F00'>*</span>收货人手机",
							allowBlank : false,
							blankText : '收货人手机不能为空',
							name : "consignPhone"
						}]
			}, {
				columnWidth : .33,
				layout : 'form',
				border : false,
				items : [{
					id : 'addOrderForm_payAmount',
					xtype : 'numberfield',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>支付金额",
					labelStyle : "text-align:right;",
					allowNegative : false, // 禁用负数
					name : 'payAmount',
					decimalPrecision : 2,
					displayed : false,
					listeners : {
						"afterrender" : function(comp, eOpts) {
							comp.getEl().up('.x-form-item').setDisplayed(false);
						}
					}
				}]
			},{
				columnWidth : .33,
				layout : 'form',
				border : false,
				items : [{
					id : 'addOrderForm_companyName',
					xtype : 'textfield',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>试吃公司名称",
					labelStyle : "text-align:right;",
					name : 'companyName',
					displayed : false,
					allowBlank : false,
					blankText : '试吃公司名称不能为空',
					listeners : {
						"afterrender" : function(comp, eOpts) {
							comp.getEl().up('.x-form-item').setDisplayed(false);
						}
					}
				}]
			}]
		}, {
			layout : 'column',
			border : false,
			items : [{
				columnWidth : .33,
				layout : 'form',
				border : false,
				items : [{
							xtype : 'hidden',
							name : 'cityName',
							value : '上海市'
						}, {
							xtype : 'dataDictionaryCmb',
							fieldLabel : "<span style='color:#F00'>*</span>配送区域",
							allowBlank : false,
							blankText : '配送区域不能为空',
							editable : false,
							autoLoad : false,
							defaultFirstRow : 1,
							anchor : '100%',
							hiddenName : 'districtName',
							dictionaryType : PmsUtil.district_name
						}]
			}, {
				columnWidth : .33,
				layout : 'form',
				border : false,
				items : [{
							xtype : 'datefield',
							fieldLabel : "<span style='color:#F00'>*</span>配送日期",
							allowBlank : false,
							blankText : '配送日期不能为空',
							editable : false,
							format : "Y-m-d",
							anchor : "100%",
							name : "deliveryDate"
						}]
			}, {
				columnWidth : .33,
				layout : 'form',
				border : false,
				items : [{
							xtype : 'dataDictionaryCmb',
							fieldLabel : "<span style='color:#F00'>*</span>配送时间段",
							editable : false,
							autoLoad : false,
							allowBlank : false,
							blankText : '配送时间段不能为空',
							defaultFirstRow : 1,
							anchor : '100%',
							hiddenName : 'deliveryTimes',
							dictionaryType : PmsUtil.orders_delivery_times
						}]
			}]
		}, {
			layout : 'column',
			border : false,
			items : [{
				columnWidth : .33,
				layout : 'form',
				border : false,
				items : [{
							id : 'addOrderForm_applyUserName',
							xtype : 'textfield',
							anchor : '100%',
							fieldLabel : "<span style='color:#F00'>*</span>试吃申请人",
							allowBlank : false,
							blankText : '试吃申请人不能为空',
							name : "applyUserName",
							displayed : false,
							listeners : {
								"afterrender" : function(comp, eOpts) {
									comp.getEl().up('.x-form-item').setDisplayed(false);
								}
							}
						}]
			}, {
				columnWidth : .33,
				layout : 'form',
				border : false,
				items : [{
							id : 'addOrderForm_applyUserPhone',
							xtype : 'textfield',
							anchor : '100%',
							fieldLabel : "<span style='color:#F00'>*</span>试吃申请人电话",
							allowBlank : false,
							blankText : '试吃申请人电话不能为空',
							name : "applyUserPhone",
							displayed : false,
							listeners : {
								"afterrender" : function(comp, eOpts) {
									comp.getEl().up('.x-form-item').setDisplayed(false);
								}
							}
						}]
			}, {
				columnWidth : .33,
				layout : 'form',
				border : false,
				items : [{
					id : 'addOrderForm_companyScale',
					xtype : 'dataDictionaryCmb',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>试吃公司规模",
					labelStyle : "text-align:right;",
					hiddenName : 'companyScale',
					displayed : false,
					defaultFirstRow : 1,
					dictionaryType : PmsUtil.company_scale,
					listeners : {
						"afterrender" : function(comp, eOpts) {
							comp.getEl().up('.x-form-item').setDisplayed(false);
						}
					}
				}]
			}]
		}, {
			layout : 'column',
			border : false,
			items : [{
				columnWidth : .99,
				layout : 'form',
				border : false,
				items : [{
							xtype : 'textfield',
							anchor : '100%',
							fieldLabel : "<span style='color:#F00'>*</span>详细地址",
							allowBlank : false,
							blankText : '详细地址不能为空',
							name : "address"
						}]
			}]
		}, {
			layout : 'column',
			border : false,
			items : [{
						columnWidth : .99,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'textfield',
									anchor : '100%',
									fieldLabel : "生日卡片",
									name : "birthdayCard"
								}]
					}]
		}, {
			layout : 'column',
			border : false,
			items : [{
						columnWidth : 1,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'textarea',
									anchor : '100%',
									fieldLabel : "备注",
									allowBlank : true,
									name : "remark"
								}]
					}]
		}]
	});

	// 定义分析器
	var goodOrderManagerReader = new Ext.data.JsonReader({
				totalProperty : "totalCount",
				root : "list",
				fields : [{
							name : "goodsId"
						}, {
							name : "goodsName"
						}, {
							name : "goodsCode"
						}, {
							name : "goodsSize"
						}, {
							name : "goodsPrice"
						}, {
							name : "goodsWeight"
						}, {
							name : "giveLaidNumber"
						}, {
							name : "goodsNumber"
						}, {
							name : "goodsStandardCode"
						}, {
							name : "goodsUnits"
						}]
			});
	// 取数据store
	var orderViewStore = new Ext.data.Store({
				autoLoad : true,
				proxy : new Ext.data.HttpProxy({
							url : __ctxPath + "/orderManager/selectGoodsPrices"
						}),
				remoteSort : false,
				baseParams : {
					start : 0,
					limit : 50
				},
				reader : goodOrderManagerReader
			});

	var sm = new Ext.grid.CheckboxSelectionModel();// 创建选择模式对象
	var cm = new Ext.grid.ColumnModel([sm, new Ext.grid.RowNumberer(), {
				header : "商品编号",
				sortable : true,
				width : 70,
				dataIndex : "goodsId"
			}, {
				header : "商品名称",
				sortable : true,
				width : 130,
				dataIndex : "goodsName"
			}, {
				header : "商品重量",
				sortable : true,
				width : 70,
				dataIndex : "goodsWeight"
			}, {
				header : "商品尺寸",
				width : 100,
				dataIndex : "goodsSize"
			}, {
				header : "商品单价",
				sortable : true,
				width : 70,
				dataIndex : "goodsPrice"
			}, {
				header : "商品数量<span style='color:#F00'>(可编辑)</span>",
				sortable : true,
				width : 120,
				dataIndex : "goodsNumber",
				editor : new Ext.form.NumberField({})
			}, {
				header : "商品编码",
				hidden : true,
				dataIndex : "goodsCode"

			}, {
				header : "赠送餐具数量",
				hidden : true,
				dataIndex : "giveLaidNumber"

			}, {
				header : "商品规格编码",
				hidden : true,
				dataIndex : "goodsStandardCode"
			}]);

	var grid = new Ext.grid.EditorGridPanel({
				id : 'grid',
				region : 'north',
				store : orderViewStore,
				clicksToEdit : 1,
				cm : cm,
				sm : sm,
				width : 300,
				frame : true,
				height : 330,
				viewConfig : {
					emptyText : '没有您想要的数据!',
					// forceFit : true,
					scrollOffset : 0
				},
				bbar : new PG.PagingBar({
							store : orderViewStore,
							isShowTwoHunder : true
						})

			});
	var smodel = grid.getSelectionModel();
	smodel.onEditorKey = function(field, e) {
		var k = e.getKey(), newCell, g = smodel.grid, ed = g.activeEditor;
		if (k == e.TAB) {
			e.stopEvent();
			ed.completeEdit();
			if (e.shiftKey) {
				newCell = g.walkCells(ed.row, ed.col - 1, -1,
						smodel.acceptsNav, smodel);
			} else {
				newCell = g.walkCells(ed.row, ed.col + 1, 1, smodel.acceptsNav,
						smodel);
			}
			if (ed.col == 1) {
				if (e.shiftKey) {
					newCell = g.walkCells(ed.row, ed.col + 1, -1,
							smodel.acceptsNav, smodel);
				} else {
					newCell = g.walkCells(ed.row, ed.col + 1, 1,
							smodel.acceptsNav, smodel);
				}
			}
		} else if (k == e.UP) {
			e.stopEvent();
			ed.completeEdit();
			newCell = g.walkCells(ed.row - 1, ed.col, -1, smodel.acceptsNav,
					smodel);
			if (ed.col == 1) {
				newCell = g.walkCells(ed.row, ed.col + 1, -1,
						smodel.acceptsNav, smodel);
			}
		} else if (k == e.DOWN) {
			e.stopEvent();
			ed.completeEdit();
			newCell = g.walkCells(ed.row + 1, ed.col, 1, smodel.acceptsNav,
					smodel);
			if (ed.col == 1) {
				newCell = g.walkCells(ed.row, ed.col + 1, 1, smodel.acceptsNav,
						smodel);
			}
		}

		if (newCell) {
			g.startEditing(newCell[0], newCell[1]);
		}
	};

	var win = new Ext.Window({
				id : 'win',
				title : title,
				layout : 'border',
				width : 700,
				height : 610,
				closeAction : 'destroy',// 关闭窗口时渐渐缩小
				closable : true,
				plain : true,
				modal : true,
				items : [grid, form], // 嵌入表单;
				buttonAlign : 'center',
				// layout : 'fit',
				buttons : [{
							id : 'addOrUpdCommit',
							text : '确定',
							formBind : true,
							handler : function() {
								insertOrder();
							}
						}, {
							text : "取消",
							handler : function() {
								win.close();
							}
						}]
			});

	return win;
}
function insertOrder() {
	var goodOrderForm = Ext.getCmp("form");
	var goodOrderGrid = Ext.getCmp("grid");
	var goodOrderGirdModel = goodOrderGrid.getSelectionModel().getSelections();
	if (goodOrderGirdModel.length == 0) {
		Ext.MessageBox.show({
					title : '提示',
					msg : '请选择商品',
					buttons : Ext.MessageBox.OK,
					icon : Ext.MessageBox.WARNING
				});
		return;
	}
	var goodsOrderArr = new Array();
	var orderMessage = "";
	var m = 0;
	for (var i = 0; i < goodOrderGirdModel.length; i++) {
		var data = goodOrderGirdModel[i].data;
		var temrecord = new Ext.data.Record();
		temrecord.set("goodsId", data.goodsId);
		temrecord.set("goodsName", data.goodsName);
		temrecord.set("goodsPrice", data.goodsPrice);
		temrecord.set("goodsSize", data.goodsSize);
		temrecord.set("goodsWeight", data.goodsWeight);
		temrecord.set("goodsNumber", data.goodsNumber);
		temrecord.set("goodsUnits", data.goodsUnits);
		temrecord.set("goodsCode", data.goodsCode);
		temrecord.set("goodsStandardCode", data.goodsStandardCode);
		temrecord.set("giveLaidNumber", data.giveLaidNumber);
		goodsOrderArr.push(temrecord.data);
		if (m > 0) {
			orderMessage = orderMessage + "," + data.goodsName + "("
					+ data.goodsWeight + ")X" + data.goodsNumber;
		} else {
			orderMessage = data.goodsName + "(" + data.goodsWeight + ")X"
					+ data.goodsNumber;
		}
		m++;
	}
	var jsonList = Ext.encode(goodsOrderArr);

	if (goodOrderForm.getForm().isValid()) {
		var  orderFinish=Ext.getCmp("addOrderForm_orderFinish").getValue();
		var  message="";
		if(orderFinish=='1'){//订单完成
			message="<span style='color:#F00'>订单下单后将直接变为完成状态</span>,";
		}
		Ext.Msg.confirm("提示框", "您选择的商品为<span style='color:#F00'>"
						+ orderMessage + "</span>,"+message+" 确认下单？", function(button) {
					if (button == "yes") {

						goodOrderForm.getForm().submit({
							url : __ctxPath + '/orderManager/saveOrder',
							params : {
								jsonList : Ext.util.Format.htmlDecode(jsonList)
										.trim()

							},
							method : "POST",
							waitTitle : "请稍候",
							waitMsg : "正在提交表单，请稍候......",
							waitTitle : "请稍候",
							root : "data",
							failure : function(response, options) {
								var jsonData = Ext.util.JSON
										.decode(options.response.responseText);
								Ext.MessageBox.show({
											title : "错误提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.ERROR
										});
							},
							success : function(response, options) {

								var jsonData = Ext.util.JSON
										.decode(options.response.responseText);
								Ext.MessageBox.show({
											title : "信息提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.INFO
										});
								Ext.getCmp("win").close();
								var gridPanel = Ext.getCmp("orderManagerGrid");
								if (typeof(gridPanel) != 'undefined') {
									gridPanel.store.reload();
								}
							}
						});

					}

				});
	}
};

function addOrderTypeShow(orderType) {
	var payAmount = Ext.getCmp("addOrderForm_payAmount");
	var companyName = Ext.getCmp("addOrderForm_companyName");
	var applyUserName = Ext.getCmp("addOrderForm_applyUserName");
	var applyUserPhone = Ext.getCmp("addOrderForm_applyUserPhone");
	var companyScale = Ext.getCmp("addOrderForm_companyScale");
	if (orderType == 6 || orderType == 7 || orderType == 8) {// 6.点评(美团)订单
		payAmount.getEl().up('.x-form-item').setDisplayed(true);
		payAmount.allowBlank = false;
		companyName.getEl().up('.x-form-item').setDisplayed(false);
		companyName.allowBlank = true;
		applyUserName.getEl().up('.x-form-item').setDisplayed(false);
		applyUserName.allowBlank = true;
		applyUserPhone.getEl().up('.x-form-item').setDisplayed(false);
		applyUserPhone.allowBlank = true;
		companyScale.getEl().up('.x-form-item').setDisplayed(false);
		companyScale.allowBlank = true;
	} else if(orderType == 2){// 试吃订单
		companyName.getEl().up('.x-form-item').setDisplayed(true);
		companyName.allowBlank = false;
		applyUserName.getEl().up('.x-form-item').setDisplayed(true);
		applyUserName.allowBlank = false;
		applyUserPhone.getEl().up('.x-form-item').setDisplayed(true);
		applyUserPhone.allowBlank = false;
		companyScale.getEl().up('.x-form-item').setDisplayed(true);
		companyScale.allowBlank = false;
		payAmount.getEl().up('.x-form-item').setDisplayed(false);
		payAmount.allowBlank = true;
	}else {
		payAmount.getEl().up('.x-form-item').setDisplayed(false);
		payAmount.allowBlank = true;
		companyName.getEl().up('.x-form-item').setDisplayed(false);
		companyName.allowBlank = true;
		applyUserName.getEl().up('.x-form-item').setDisplayed(false);
		applyUserName.allowBlank = true;
		applyUserPhone.getEl().up('.x-form-item').setDisplayed(false);
		applyUserPhone.allowBlank = true;
		companyScale.getEl().up('.x-form-item').setDisplayed(false);
		companyScale.allowBlank = true;
	}
}
